" Install vim-plug if not found
if empty(glob('~/.vim/autoload/plug.vim'))
  silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs
    \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
endif

" Run PlugInstall if there are missing plugins
autocmd VimEnter * if len(filter(values(g:plugs), '!isdirectory(v:val.dir)'))
  \| PlugInstall --sync | source $MYVIMRC
\| endif

set rtp+=~/.fzf

call plug#begin()

" Colored indentation levels
Plug 'nathanaelkane/vim-indent-guides'

" Turn your browser¹ into a Neovim client
"Plug 'glacambre/firenvim', { 'do': { _ -> firenvim#install(0) } }

" highlight words in different colors simultaneously
Plug 'https://github.com/inkarkat/vim-ingo-library'
  Plug 'https://github.com/inkarkat/vim-mark'

Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
  " Fuzzy-finder commands
  Plug 'junegunn/fzf.vim'

" My plugin that persists open file list in .buffers.vim file
Plug 'https://github.com/dmos62/vim-buffer-persist'

" Insert mode tab complete
Plug 'https://github.com/ervandew/supertab'

" Improve vim's text-objects
Plug 'https://github.com/paradigm/TextObjectify'

" Improve netrw
Plug 'https://github.com/tpope/vim-vinegar'

" Safe/universal vim defaults
Plug 'https://github.com/tpope/vim-sensible'


Plug 'https://github.com/flazz/vim-colorschemes'
Plug 'https://github.com/xolox/vim-misc' " vim-colorscheme-switcher dependency
  Plug 'https://github.com/xolox/vim-colorscheme-switcher' " f8 & s-f8
" Provides :XtermColorTable for color debugging or reference
Plug 'https://github.com/guns/xterm-color-table.vim'

  " EditorConfig support
Plug 'editorconfig/editorconfig-vim'

" Ag support
Plug 'mileszs/ack.vim'
Plug 'Vimjas/vim-python-pep8-indent'

" Allows editing files via quickfix; e.g. use Rg to find occurances and edit
" them directly in quickfix
Plug 'stefandtw/quickfix-reflector.vim'

" Rg support
Plug 'jremmen/vim-ripgrep'

" high contrast themes
Plug 'https://gitlab.com/protesilaos/tempus-themes-vim.git'

if !has('nvim')
  Plug 'dense-analysis/ale'

endif
if has('nvim')
  Plug 'nvim-treesitter/nvim-treesitter', {'do': ':TSUpdate'}
  Plug 'neovim/nvim-lspconfig'
  Plug 'gfanto/fzf-lsp.nvim'

  "Plug 'folke/trouble.nvim'
  Plug 'folke/tokyonight.nvim', { 'branch': 'main' }

  " Pinned to avoid breaking changes
  " NOTE may require recent version of neovim
  " https://github.com/smoka7/hop.nvim/wiki
  Plug 'smoka7/hop.nvim', { 'tag': 'v2.3.1' }
endif

" have tempus themes enforce a background
" doesn't do that by default to support transparent terminal emulators
" see https://gitlab.com/protesilaos/tempus-themes-vim
let g:tempus_enforce_background_color=1

" devicon setup
if has('nvim')
  Plug 'kyazdani42/nvim-web-devicons'
else
  set encoding=UTF-8
  Plug 'ryanoasis/vim-devicons'
endif
call plug#end()

if has('nvim')
  " setup hop.nvim
  " https://github.com/smoka7/hop.nvim/wiki/Configuration
  lua require'hop'.setup ()
  nmap s1 <Esc>:HopChar1<CR>
  nmap s2 <Esc>:HopChar2<CR>
  nmap sl <Esc>:HopLineStart<CR>
  nmap sw <Esc>:HopWord<CR>
endif

" kad automatiskai pradetu veikti 'nathanaelkane/vim-indent-guides'
let g:indent_guides_enable_on_vim_startup = 1

" helps avoid hit-enter prompts
set cmdheight=2

" stulpelis kaireje kuriame pazymetos klaidos
"set signcolumn=auto:1-3
set signcolumn=no

"vim-mark kad nereiketu rasyti viso MarkClear
command Mc MarkClear
command Nc noh

"let g:ale_linters = {'svelte': ['svelteserver', 'eslint', 'tsserver']}
"let g:ale_linters = {'svelte': ['tsserver']}



" vim-mark kad lengviau markinti butu
map m \m
" vim-mark extended (18 color) palette
"let g:mwDefaultHighlightingPalette = 'extended'

autocmd FileType hledger setlocal omnifunc=hledger#complete#omnifunc

autocmd FileType go setlocal tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab

" change vim-colorscheme-switcher mappings
let g:colorscheme_switcher_define_mappings = 0
nnoremap <silent> <F7> :PrevColorScheme<CR>
nnoremap <silent> <F8> :NextColorScheme<CR>
nnoremap <silent> <F9> :RandomColorScheme<CR>

set wildmenu
syntax on
filetype plugin indent on

"kad veiktu 256 spalvos
let &t_Co=256
let &t_AF="\e[38;5;%dm"
let &t_AB="\e[48;5;%dm"

"kad geriau veiktu kursorius
let &t_ti.="\e[1 q"
let &t_SI.="\e[5 q"
let &t_EI.="\e[1 q"
let &t_te.="\e[0 q"

"normalus indentas
set expandtab
set shiftwidth=2
set softtabstop=2

set encoding=utf-8
set fileencoding=utf-8

map <F1> <Esc>:w<CR>
map <F4> <Esc>:tabnew<CR>
"nmap <F9> <Esc>:source ~/.vimrc<CR>
nmap <F10> <Esc>:tabnew ~/.vimrc<CR>
" naudok fzf geriau
"nmap <F6> :FufTag<CR>
"nmap <F7> :FufCoverageFile<CR>
"nmap <F8> :FufCoverageFileChange scala<CR>

" kad rastu tagus
set tags=./tags,./../tags,./../../tags

" naudoja fuzzyfinder vietoj paprasto C-]
" noremap <silent> <C-]> :FufTagWithCursorWord!<CR>

" pazymi kur per ilgos linijos kerta riba
" augroup collumnLimit
"   autocmd!
"   autocmd BufEnter,WinEnter,FileType scala,java
"         \ highlight CollumnLimit ctermbg=DarkGrey guibg=DarkGrey
"   let collumnLimit = 79
"   let pattern = 
"         \ '\%<' . (collumnLimit+1) . 'v.\%>' . collumnLimit . 'v'
"   autocmd BufEnter,WinEnter,FileType scala,java
"         \ let w:m1=matchadd('CollumnLimit', pattern, -1)
" augroup END

" kad scaloj newrapintu keistai
" augroup scala_fixai
"   autocmd!
"   autocmd FileType scala set fo-=t fo-=r
" augroup END

" fzf : https://github.com/junegunn/fzf
" set rtp+=~/.fzf

" Show syntax highlighting groups for word under cursor
" " geresnis variantas https://github.com/gerw/vim-HiLinkTrace :h hilinks
" nmap <Leader>c :call <SID>SynStack()<CR>
" function! <SID>SynStack()
"   if !exists("*synstack")
"     return
"   endif
"   echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')
" endfun

" Mano scheme'as
"colorscheme volvo

let g:textobjectify = {
      \'(': {'left': '(', 'right': ')', 'same': 0, 'seek': 1, 'line': 0},
      \')': {'left': '(', 'right': ')', 'same': 0, 'seek': 2, 'line': 0},
      \'{': {'left': '{', 'right': '}', 'same': 0, 'seek': 1, 'line': 0},
      \'}': {'left': '{', 'right': '}', 'same': 0, 'seek': 2, 'line': 0},
      \'[': {'left': '\[', 'right': '\]', 'same': 0, 'seek': 1, 'line': 0},
      \']': {'left': '\[', 'right': '\]', 'same': 0, 'seek': 2, 'line': 0},
      \'<': {'left': '<', 'right': '>', 'same': 0, 'seek': 1, 'line': 0},
      \'>': {'left': '<', 'right': '>', 'same': 0, 'seek': 2, 'line': 0},
      \'"': {'left': '"', 'right': '"', 'same': 1, 'seek': 1, 'line': 0},
      \"'": {'left': "'", 'right': "'", 'same': 1, 'seek': 1, 'line': 0},
      \'`': {'left': '`', 'right': '`', 'same': 1, 'seek': 1, 'line': 0},
      \'V': {'left': '^\s*\(if\|for\|function\|try\|while\)\>',
      \'right': '^\s*end', 'same': 0, 'seek': 1, 'line': 1},
      \"\<cr>": {'left': '\%^', 'right': '\%$', 'same': 0, 'seek': 0, 
      \'line': 0},
      \}

" let g:clojure_align_multiline_strings = 1

" let g:clojure_fuzzy_indent = 0
" let g:clojure_fuzzy_indent_patterns = ['^with', '^def', '^let']
" let g:clojure_fuzzy_indent_blacklist = ['-fn$', '\v^with-%(meta|out-str|loading-context)$']

set breakindent briopt=shift:1

" Kad paspaudus shift-k neismestu is vimo
noremap K <NOP>

" bundle/buffer-persist
" jeigu pradeta sesija issaugo buferius.
" uzsikraunant jei randa buffer faila uzkrauna issaugotus buferius
let g:BufferFile = ".buffers.vim"
command StartSession call StartBufferPersistSession()

au BufRead,BufNewFile *.cljc setfiletype clojure

set modeline

" sutrumpinimai
iab abdate <c-r>=strftime("%y-%m-%d")<cr>
iab abtime <c-r>=strftime("%R")<cr>

" paieskoms su /.
set ignorecase " jeigu ieskai mazosiomis, iesko visais raidziu dydziais
set smartcase " jeigu naudoji didziasias, tada pasidaro case-sensitive
" galima naudoti \C paieskoje kad ijungti case-sensitivity arba \c kad ijungti

set mouse= " apeina bug'a kai vim'as nesekmingai bendrauja su gpm

" where to put swap files
set directory=~/.vim/tmp

" create tilde files (*~), before write, but delete after successful write
set nobackup
set writebackup

" Initialize configuration dictionary
let g:fzf_vim = {}

" Empty value to disable preview window altogether
let g:fzf_vim.preview_window = []

" [Tags] Command to generate tags file
let g:fzf_vim.tags_command = 'ctags -R'

"color tempus_dawn " light
"color tempus_tempest " dark

" hybrid (relative + absolute for current line) line numbers
set number relativenumber

" auto-indent new lines
set autoindent

" below indentation options are possibly smarter, but also might be too
" complicated
"set smartindent
"set cindent

set hls

" highlight trailing whitespace
highlight ExtraWhitespace ctermbg=blue guibg=blue
match ExtraWhitespace /\s\+$/
autocmd BufWinEnter * match ExtraWhitespace /\s\+$/
autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
autocmd InsertLeave * match ExtraWhitespace /\s\+$/
autocmd BufWinLeave * call clearmatches()

" enable hybrid line numbers
" see https://jeffkreeftmeijer.com/vim-number/
set number relativenumber

" light, high contrast
"color tempus_fugit

" dark, high contrast 
"color tempus_night

command CurrentTheme source ~/.vim/theme/current
CurrentTheme
